package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.entity.Comment;
import com.sangeng.domain.vo.CommentVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.exception.SystemException;
import com.sangeng.mapper.CommentMapper;
import com.sangeng.service.CommentService;
import com.sangeng.service.UserService;
import com.sangeng.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(com.sangeng.domain.entity.Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-05-15 16:38:38
 */
@Service("sgCommentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commentList(Integer pageNum, Integer pageSize, Long articleId) {
        /**查询文章对应的根部评论，封装成ResponseResult对象返回*/
        //查询文章对应的根评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getArticleId, articleId);
        wrapper.eq(Comment::getRootId, -1);
        //分页查询
        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page,wrapper);
        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());
        list(wrapper);

/**抽取出方法toCommentVoList        //bean拷贝，给评论人name赋值-昵称
        List<CommentVo> commentVoList = BeanCopyUtils.copyBeanList(page.getRecords(), CommentVo.class);*/

        //查询所有根评论对应的子评论集合，并赋给对应的属性
        for (CommentVo commentVo : commentVoList) {
            //查询所有对应的子评论 . 同样抽取出方法
            List<CommentVo> children=getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);
        }

        //封装成ResponseResult对象返回
        return  ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        //评论内容不能为空
        if (!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        //从token中获取信息 ,使用工具类SecurityUtils
        /**comment.setCreateBy(SecurityUtils.getUserId()); 已经配置自动填充
         * createBy,createTime 插入时填充
         * updateBy，updateTime更新时填充
         * */
        save(comment);
        return ResponseResult.okResult();
    }
    /**友链 */
    @Override
    public ResponseResult linkCommentList(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getType,1);
        wrapper.eq(Comment::getRootId,-1);
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page, wrapper);

        List<CommentVo> commentVos = toCommentVoList(page.getRecords());

        for (CommentVo commentVo : commentVos) {
            //查询所有对应的子评论 . 同样抽取出方法
            List<CommentVo> children=getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);
        }

        return ResponseResult.okResult(new PageVo(commentVos,page.getTotal()));
    }


    /**
     * 根据根评论的id查询所对应的子评论的集合
     * @param id 根评论的id
     * @return VoList
     * */
    private List<CommentVo> getChildren(Long id){
        //查询所有子评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getRootId, id);
        wrapper.orderByAsc(Comment::getCreateTime);

        List<Comment> comments = list(wrapper);

        List<CommentVo> commentVos = toCommentVoList(comments);

        return commentVos;
    }


    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        //遍历Vo集合
        for (CommentVo commentVo : commentVos) {
            //通过createBy查询用户昵称，并赋值
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);
            //通过toCommentUserId查询用户昵称，并赋值 ; 此项值不为-1时才查询
            if (commentVo.getToCommentUserId() != -1){
                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserName);
            }
        }
        //返回集合
        return commentVos;
    }
}

